home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sndhrdw / cchasm.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  4KB  |  201 lines

  1. /*
  2.  * Cosmic Chasm sound and other IO hardware emulation
  3.  *
  4.  * Jul 15 1999 by Mathis Rosenhauer
  5.  *
  6.  */
  7.  
  8. #include "driver.h"
  9. #include "cpu/z80/z80.h"
  10. #include "machine/z80fmly.h"
  11.  
  12. static int sound_status[2];
  13. static int sound_command[2];
  14. static int sound_flags;
  15.  
  16. READ_HANDLER( cchasm_snd_io_r )
  17. {
  18.     int coin;
  19.  
  20.     switch (offset & 0x61 )
  21.     {
  22.     case 0x00:
  23.         coin = (input_port_3_r (offset) >> 4) & 0x7;
  24.         if (coin != 0x7) coin |= 0x8;
  25.         return sound_flags | coin;
  26.  
  27.     case 0x01:
  28.         return AY8910_read_port_0_r (offset);
  29.  
  30.     case 0x21:
  31.         return AY8910_read_port_1_r (offset);
  32.  
  33.     case 0x40:
  34.         return sound_command[0] & 0xff;
  35.  
  36.     case 0x41:
  37.         sound_flags &= ~0x80;
  38.         z80ctc_0_trg2_w (0, 0);
  39.         return sound_command[1] & 0xff;
  40.     default:
  41.         logerror("Read from unmapped internal IO device at 0x%x\n", offset + 0x6000);
  42.         return 0;
  43.     }
  44. }
  45.  
  46. WRITE_HANDLER( cchasm_snd_io_w )
  47. {
  48.     switch (offset & 0x61 )
  49.     {
  50.     case 0x00:
  51.         AY8910_control_port_0_w (offset, data);
  52.         break;
  53.  
  54.     case 0x01:
  55.         AY8910_write_port_0_w (offset, data);
  56.         break;
  57.  
  58.     case 0x20:
  59.         AY8910_control_port_1_w (offset, data);
  60.         break;
  61.  
  62.     case 0x21:
  63.         AY8910_write_port_1_w (offset, data);
  64.         break;
  65.  
  66.     case 0x40:
  67.         sound_status[0] = data;
  68.         break;
  69.  
  70.     case 0x41:
  71.         sound_flags |= 0x40;
  72.         sound_status[1] = data;
  73.         cpu_cause_interrupt(0,1);
  74.         break;
  75.  
  76.     case 0x61:
  77.         z80ctc_0_trg0_w (0, 0);
  78.         break;
  79.  
  80.     default:
  81.         logerror("Write %x to unmapped internal IO device at 0x%x\n", data, offset + 0x6000);
  82.     }
  83. }
  84.  
  85. WRITE_HANDLER( cchasm_io_w )
  86. {
  87.     static int led;
  88.  
  89.     switch ((offset >> 1) & 0xf)
  90.     {
  91.     case 0:
  92.         sound_command[0] = data >> 8;
  93.         break;
  94.     case 1:
  95.         sound_flags |= 0x80;
  96.         sound_command[1] = data >> 8;
  97.         z80ctc_0_trg2_w (0, 1);
  98.         cpu_cause_interrupt( 1, Z80_NMI_INT );
  99.         break;
  100.     case 2:
  101.         led = data;
  102.         break;
  103.     }
  104. }
  105.  
  106. READ_HANDLER( cchasm_io_r )
  107. {
  108.     switch ((offset >> 1) & 0xf)
  109.     {
  110.     case 0x0:
  111.         return sound_status[0] << 8;
  112.     case 0x1:
  113.         sound_flags &= ~0x40;
  114.         return sound_status[1] << 8;
  115.     case 0x2:
  116.         return (sound_flags| (input_port_3_r (offset) & 0x07) | 0x08) << 8;
  117.     case 0x5:
  118.         return input_port_2_r (offset) << 8;
  119.     case 0x8:
  120.         return input_port_1_r (offset) << 8;
  121.     default:
  122.         return 0xff << 8;
  123.     }
  124. }
  125.  
  126. static int channel[2], channel_active[2];
  127. static int output[2];
  128.  
  129. static void ctc_interrupt (int state)
  130. {
  131.     cpu_cause_interrupt (1, Z80_VECTOR(0,state) );
  132. }
  133.  
  134. static WRITE_HANDLER( ctc_timer_1_w )
  135. {
  136.  
  137.     if (data) /* rising edge */
  138.     {
  139.         output[0] ^= 0x7f;
  140.         channel_active[0] = 1;
  141.         stream_update(channel[0], 0);
  142.     }
  143. }
  144.  
  145. static WRITE_HANDLER( ctc_timer_2_w )
  146. {
  147.  
  148.     if (data) /* rising edge */
  149.     {
  150.         output[1] ^= 0x7f;
  151.         channel_active[1] = 1;
  152.         stream_update(channel[1], 0);
  153.     }
  154. }
  155.  
  156. static z80ctc_interface ctc_intf =
  157. {
  158.     1,                   /* 1 chip */
  159.     { 0 },               /* clock (filled in from the CPU 0 clock */
  160.     { 0 },               /* timer disables */
  161.     { ctc_interrupt },   /* interrupt handler */
  162.     { 0 },               /* ZC/TO0 callback */
  163.     { ctc_timer_1_w },     /* ZC/TO1 callback */
  164.     { ctc_timer_2_w }      /* ZC/TO2 callback */
  165. };
  166.  
  167. static void tone_update(int num,INT16 *buffer,int length)
  168. {
  169.     INT16 out = 0;
  170.  
  171.     if (channel_active[num])
  172.         out = output[num] << 8;
  173.  
  174.     while (length--) *(buffer++) = out;
  175.     channel_active[num] = 0;
  176. }
  177.  
  178. int cchasm_sh_start(const struct MachineSound *msound)
  179. {
  180.     sound_status[0] = 0; sound_status[1] = 0;
  181.     sound_command[0] = 0; sound_command[1] = 0;
  182.     sound_flags = 0;
  183.     output[0] = 0; output[1] = 0;
  184.  
  185.     channel[0] = stream_init("CTC sound 1", 50, Machine->sample_rate, 0, tone_update);
  186.     channel[1] = stream_init("CTC sound 2", 50, Machine->sample_rate, 1, tone_update);
  187.  
  188.     ctc_intf.baseclock[0] = Machine->drv->cpu[1].cpu_clock;
  189.     z80ctc_init (&ctc_intf);
  190.  
  191.     return 0;
  192. }
  193.  
  194. void cchasm_sh_update(void)
  195. {
  196.     if ((input_port_3_r (0) & 0x70) != 0x70)
  197.         z80ctc_0_trg0_w (0, 1);
  198. }
  199.  
  200.  
  201.